//初始化信息
let fileName;
var isContinue = true;
let routeType, routeArea, routeAbbreviation;//线路类型、区域、线路简称

async function init() {
    fileName = await window.creatRouteID_API.getFileName();
    window.creatRouteID_API.getRouteInfo().then((infos) => {
        routeType = infos[0];
        routeArea = infos[1];
        routeAbbreviation = infos[2];
    });
}
init();

//确定是否需要登录系统
setTimeout(function () {
    let divDom = document.getElementById("tab-uname");
    if (divDom && divDom.innerText == '账号密码登录') {
        autoLogin();
    } else {
        setTimeout(checkTarget, 2000);
    }
}, 1000);



function autoLogin() {

    var inputEvent = new Event('input', {
        bubbles: true,
        cancelable: true
    });
    let sign1 = false, sign2 = false;
    for (const e of document.querySelectorAll('input')) {
        if (e.placeholder == '账号') {
            e.value = 'wuyunqing01';
            sign1 = true;
            e.dispatchEvent(inputEvent);
        }
        if (e.placeholder == '密码') {
            e.value = 'wuyunqing@6';
            sign2 = true;
            e.dispatchEvent(inputEvent);
        }
        if (sign1 && sign2) {
            for (const span of document.querySelectorAll('span')) {
                if (span.innerText === '登 录') {
                    span.parentElement.click();
                    setTimeout(checkTarget, 700);
                    break;
                }
            }
            break;
        }
    }
}

function checkTarget() {
    //是否继续执行
    if (fileName === null) {
        console.log("执行完毕");
        return;
    }
    let sign = true;
    for (const ele of document.querySelectorAll('span')) {
        if (ele.innerText === '发布产品') {
            let obj = ele.parentElement;
            if (obj && obj.tagName && obj.tagName.toUpperCase() === 'LI') {
                obj.click();
                setTimeout(createRouteID, 700);
                sign = false;
                break;
            }
        }
    }
    if (sign) {
        setTimeout(checkTarget, 200);
    }
}

function createRouteID() {
    //点击'线路'类型
    let sign = true;
    for (const ele of document.querySelectorAll('div')) {
        if (ele.innerText.replace(/\s|\\n/g, '') === '线路专指需要签署旅游合同，提供标准化服务的跟团旅游产品。') {
            ele.click();
            sign = false;
            //点击'下一步'
            for (const next of document.querySelectorAll('span')) {
                if (next.innerText.replace(/\s/g, '') === '下一步') {
                    console.log(next);
                    next.parentElement.click();
                    setTimeout(() => {

                        autoFillInfo(routeType, routeArea, fileName.match('【.*?出发】')[0].replace('【', '').replace('出发】', ''),
                            fileName, routeAbbreviation
                        );
                    }, 1000);
                    break;
                }
            }
            break;
        }
    }
    if (sign) {
        setTimeout(createRouteID, 200);
    }
}

//选择下拉框的复用函数
function selectOption(placeholder, targetValue) {
    var inputEvent = new Event('input', {
        bubbles: true,
        cancelable: true
    });
    const input = Array.from(document.querySelectorAll('input')).find(input => input.placeholder === placeholder);
    if (input) {
        input.dispatchEvent(inputEvent);

        const targetLi = Array.from(document.querySelectorAll('li')).find(li => li.innerHTML === targetValue);
        // console.log(targetLi);
        if (targetLi) {
            targetLi.click();
        }
    }
}

/**
 * 
 * @param {线路类型} routeType 
 * @param {线路区域} routeArea 
 * @param {出发城市} departCity 
 * @param {线路名称} routeName 
 * @param {线路简称} routeAbbreviation 
 */
function autoFillInfo(routeType, routeArea, departCity, routeName, routeAbbreviation,) {
    var inputEvent = new Event('input', {
        bubbles: true,
        cancelable: true
    });
    Promise.resolve().then(() => {
        //选择供应商
        console.log('1');
        selectOption('请选择供应商', '<span>佛山旅游有限公司</span>');
    }).then(() => {
        console.log('2');
        selectOption('请选择线路类型', '<span>' + routeType + '</span>');
    }).then(() => {
        console.log('3');
        selectOption('请选择线路区域', '<span>' + routeArea + '</span>');
    }).then(() => {
        console.log('4');
        selectOption('请选择出游方式', '<span>拼团</span>');
    }).then(() => {
        console.log('5');
        for (const ele of document.querySelectorAll('input')) {
            if (ele.placeholder === '请选择产品所在地') {
                ele.click();
                for (const spanEle of document.querySelectorAll('span')) {
                    if (spanEle.innerText === '广东省') {
                        spanEle.parentElement.click();
                        function findCity() {
                            let sign = true;
                            for (const spanEle of document.querySelectorAll('span')) {
                                if (spanEle.innerText === '佛山市') {
                                    clearInterval(findCity);
                                    spanEle.parentElement.click();
                                    sign = false;
                                    break;
                                }
                            }
                            if (sign) setTimeout(findCity, 200);
                        }
                        findCity();
                        break;
                    }
                }
                break;
            }

        }
    }).then(() => {
        console.log('6');
        const input = Array.from(document.querySelectorAll('input')).find(input => input.placeholder === '请选择出发城市');
        if (input) {
            input.dispatchEvent(inputEvent);
            const targetLi = Array.from(document.querySelectorAll('li')).find(li => li.innerHTML === '<span>' + departCity + '</span>');
            if (targetLi) {
                targetLi.click();
                setTimeout(() => {
                    input.click();
                    input.click();
                }, 500);
            }
        }
        // selectOption('请选择出发城市', '<span>佛山</span>');
    }).then(() => {
        console.log('7');
        for (const input of document.querySelectorAll('input')) {
            if (input.placeholder === '请输入产品名称') {
                input.value = routeName;
                input.dispatchEvent(inputEvent);
            }
            if (input.placeholder === '请输入产品简称') {
                input.value = routeAbbreviation;
                input.dispatchEvent(inputEvent);
            }
            if (input.placeholder === '请输入产品适用人群') {
                input.value = '中老年';
                input.dispatchEvent(inputEvent);
            }

        }
    }).then(() => {
        console.log('8');
        //添加上车点
        for (const span of document.querySelectorAll('span')) {
            if (span.innerText === '选择上车点') {
                span.parentElement.click();

                function checkInput() {
                    let sign = true;
                    //在上车点搜索栏输入目标区域
                    let targetInput = Array.from(document.querySelectorAll('input')).find(input => input.placeholder === '请输入名称');
                    if (targetInput) {
                        sign = false;
                        targetInput.value = departCity;
                        targetInput.dispatchEvent(inputEvent);
                        //点击'搜索'按钮
                        let serchBtn = Array.from(document.querySelectorAll('button')).find(btn => btn.innerText === '搜索');
                        if (serchBtn) {
                            console.log(serchBtn);
                            serchBtn.click();
                            setTimeout(() => {
                                //点击'100条/页'按钮
                                let pageNumber = Array.from(document.querySelectorAll('span')).find(span => span.innerText === '100条/页');
                                if (pageNumber) {
                                    pageNumber.click();
                                }
                                setTimeout(() => {
                                    //点击'全选'复选框
                                    let targetDiv = Array.from(document.querySelectorAll('div')).find(div => div.innerText === '序号');
                                    targetDiv.parentElement.parentElement.firstChild.firstChild.firstChild.click();
                                    let times = 0;
                                    //搜索并点击'确认'按钮
                                    for (const spanDom of document.querySelectorAll('span')) {
                                        if (spanDom.innerHTML === '确 定') {
                                            times++;
                                            if (times == 2) {
                                                setTimeout(() => {
                                                    spanDom.parentElement.click();
                                                    //搜索并点击'保存'按钮
                                                    let times = 0;
                                                    for (const spanDom of document.querySelectorAll('span')) {
                                                        if (spanDom.innerHTML === '保 存') {
                                                            times++;
                                                            if (times == 3) {
                                                                setTimeout(() => {
                                                                    spanDom.parentElement.click();
                                                                    saveRouteID();
                                                                    function saveRouteID() {
                                                                        let routeNameSpan = Array.from(document.querySelectorAll('span')).find(span => span.innerText === routeName);
                                                                        if (routeNameSpan) {
                                                                            //保存产品编号
                                                                            window.creatRouteID_API.saveRouteID_pre(departCity + routeNameSpan.parentElement.parentElement.parentElement.firstChild.firstChild.firstChild.innerText.replace('产品编号', ''));
                                                                            //刷新页面，重新执行脚本
                                                                            window.location.reload();
                                                                        } else {
                                                                            setTimeout(saveRouteID, 500);
                                                                        }
                                                                        // for (let ele of document.querySelectorAll('span')) {
                                                                        //     if (ele.innerText === routeName) {
                                                                        //         // 保存产品编号
                                                                        //         window.creatRouteID_API.saveRouteID(ele.parentElement.parentElement.parentElement.firstChild.firstChild.firstChild.innerText.replace('产品编号', ''));
                                                                        //         //刷新页面，重新执行脚本
                                                                        //         window.location.reload();
                                                                        //     }
                                                                        // }
                                                                    }

                                                                }, 500);
                                                                times = 0;
                                                                break;
                                                            }
                                                        }
                                                    }
                                                }, 800);
                                                times = 0;
                                                break;
                                            }
                                        }

                                    }
                                }, 500);
                            }, 800);
                        }
                    }
                    if (sign) setTimeout(checkInput, 200);//递归检测
                }
                checkInput();
            }
        }
    })

}




